package com.jackrain.nea.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.config.Resources;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.query.QuerySession;
import lombok.Getter;
import lombok.Setter;

/**
 * ValueHolder 封装对象
 *
 * @author Yvan
 */
public class StrongValueHolder {

    @Getter
    @Setter
    private Integer code = 0;
    @Getter
    @Setter
    private String message;
    @Getter
    @Setter
    private Object data;
    @Getter
    @Setter
    private JSONObject ret;
    @Getter
    @Setter
    private Integer success = 0;
    @Getter
    @Setter
    private Integer fail = 0;
    // 自定义返回数据,如：customData:{"ERROR_CODE":1} 生成code平级对象ERROR_CODE值为1
    @Getter
    @Setter
    private JSONObject customData;

    public StrongValueHolder() {
    }

    public StrongValueHolder(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public StrongValueHolder(Integer code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public StrongValueHolder(Integer code, String message, QuerySession session) {
        this.code = code;
        this.message = Resources.getMessage(message, session.getLocale());
    }

    /**
     * 创建新增的数据返回数据
     *
     * @param tableName 表名称
     * @param id        主键ID
     */
    public void addCreationData(String tableName, Long id) {
        JSONObject object = new JSONObject();
        object.put("tablename", tableName);
        object.put("objid", id);
        object.put("error", this.data);
        this.data = object;
    }

    /**
     * 设置错误信息
     *
     * @param message 错误信息
     */
    public void setFailMessage(String message) {
        this.code = -1;
        this.message = message;
    }

    /**
     * 设置错误信息
     *
     * @param message 错误信息
     * @param data    错误数据
     */
    public void setFailMessage(String message, Object data) {
        this.code = -1;
        this.message = message;
        this.data = data;
    }

    /**
     * 自定义失败数据 -- 依赖方法引用，不作为直接返回数据
     *
     * @param message 错误信息
     * @param id      错误对象主键ID
     */
    public void addFailData(String message, Long id) {
        this.code = -1;
        this.fail++;
        JSONArray data = this.data == null ? null : JSON.parseArray(this.data.toString());
        // 拼装错误信息对象
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("objid", id);
        jsonObject.put("code", -1);
        jsonObject.put("message", message);
        if (data == null || data.size() == 0) {
            data = new JSONArray();
        }
        data.add(jsonObject);
        this.data = data;
    }

    /**
     * 增加成功记录数
     */
    public void addSuccessData() {
        this.success++;
    }

    /**
     * 判断当前是否全部成功
     *
     * @return true 全部成功
     */
    public boolean isSuccess() {
        if (this.code < 0) {
            return false;
        }
        return true;
    }

    /**
     * 设置返回信息
     *
     * @param successMessage 成功返回信息
     * @param halfMessage    存在成功和失败信息
     * @param failMessage    失败信息
     */
    public void setCustomMessage(String successMessage, String halfMessage, String failMessage) {
        if (this.fail > 0) {
            if (this.success > 0) {
                this.message = halfMessage;
            } else {
                this.message = failMessage;
            }
        } else {
            this.message = successMessage;
        }
    }

    /**
     * 设置返回信息（带成功记录数）
     *
     * @param successMessage     成功返回信息
     * @param halfSuccessMessage 成功记录数
     * @param halfFailMessage    失败记录数
     * @param failMessage        失败信息
     */
    public void setCustomMessage(String successMessage, String halfSuccessMessage, String halfFailMessage, String failMessage) {
        if (this.fail > 0) {
            this.message = halfSuccessMessage + this.success + "\n" + halfFailMessage + this.fail;
//            if (this.success > 0) {
//                this.message = halfSuccessMessage + this.success + "\n" + halfFailMessage + this.fail;
//            } else {
//                this.message = failMessage;
//            }
        } else {
            this.message = successMessage;
        }
    }

    /**
     * 返回单对象数据信息
     *
     * @param successMessage 成功消息
     */
    public void setSingleBackMessage(String successMessage) {
        if (this.fail > 0) {
            JSONArray jsonArray = this.data == null ? null : JSON.parseArray(this.data.toString());
            if (jsonArray != null && jsonArray.size() > 0) {
                JSONObject object = JSON.parseObject(jsonArray.get(0).toString());
                this.message = object.getString("message");
            }
        } else {
            this.message = successMessage;
        }
        this.data = null;
    }

    /**
     * 返回当前结果
     */
    public ValueHolder result() {
        ValueHolder valueHolder = new ValueHolder();
        if (code != null) {
            valueHolder.put("code", code);
        }
        if (message != null) {
            valueHolder.put("message", message);
        }
        if (data != null) {
            valueHolder.put("data", data);
        }
        if (ret != null) {
            valueHolder.put("ret", ret);
        }
        if (customData != null && customData.size() > 0) {
            for(String key : customData.keySet()){
                valueHolder.put(key, customData.get(key));
            }
        }
        return valueHolder;
    }

}
